package com.easyidea.its.web.action.tracklog;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.easyidea.its.domain.model.passport.TrackLog;
import com.easyidea.its.facade.tracklog.TrackLogMgr;
import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.util.StringUtil;
import com.easyidea.its.web.action.base.ItsBaseAction;
import com.easyidea.its.web.dto.CarSeizureDto;
import com.easyidea.its.web.dto.SeesionUserDto;

public class TrackLogAction extends ItsBaseAction {

	/**
	 * 
	 */
	protected Log log = LogFactory.getLog(this.getClass());

	private static final long serialVersionUID = 1L;

	private String firstPicPath;

	private Integer width = 400;

	private Integer height = 300;

	private String searchLeastRecord = "true";

	private Integer tracklogIdIndex;

	private Integer cPage;

	private boolean hasPrev = false;

	private boolean hasNext = false;

	private Integer totalNum;

	private TrackLogMgr trackLogMgr;

	private TrackLog trackLog;

	private String passPortName;

	private String direction;

	public String getPassPortName() {
		passPortName = StringUtil.strEncode(passPortName);
		// System.out.println("get passPortName  :" + passPortName);
		return passPortName;
	}

	public void setPassPortName(String passPortName) {
		passPortName = StringUtil.strDecode(passPortName);
		this.passPortName = passPortName;
	}

	public String getDirection() {
		direction = StringUtil.strEncode(direction);
		return direction;
	}

	public void setDirection(String direction) {
		direction = StringUtil.strDecode(direction);
		this.direction = direction;
	}

	public String tracklogTabbar() {
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String tracklog() {
		SeesionUserDto dto = (SeesionUserDto) getSession().getAttribute(ItsConstants.SESSION_KEY_USER_INFO);
		if (tracklogIdIndex != null) {

			List<TrackLog> tracklogs = null;
			if (getSession().getAttribute(dto.getUserId() + "." + ItsConstants.SESSION_KEY_TRACKLOG_INFO) != null) {
				tracklogs = (List<TrackLog>) getSession().getAttribute(
						dto.getUserId() + "." + ItsConstants.SESSION_KEY_TRACKLOG_INFO);
			} else if (getSession().getAttribute(dto.getUserId() + "." + ItsConstants.SESSION_KEY_CAR_SEIZURE) != null) {
				CarSeizureDto carSeizureDto = (CarSeizureDto) getSession().getAttribute(
						dto.getUserId() + "." + ItsConstants.SESSION_KEY_CAR_SEIZURE);
				tracklogs = trackLogMgr.getTrackLogsByCondition(carSeizureDto, 0, 0);
				getSession().setAttribute(dto.getUserId() + "." + ItsConstants.SESSION_KEY_TRACKLOG_INFO, tracklogs);
			}
			totalNum = tracklogs.size();
			hasPrev = tracklogIdIndex == 0 ? true : false;
			hasNext = tracklogIdIndex + 1 >= tracklogs.size() ? true : false;
			trackLog = tracklogs.get(tracklogIdIndex);
		} else {
			trackLog = this.trackLogMgr.getLatestTrackLogByCondition(passPortName, direction, searchLeastRecord
					.equals("true"), null, null);

		}
		if(trackLog != null) {
			setFirstPicPath(trackLog.getFirstPicPath());
		}
		// getSession().setAttribute(dto.getUserId() +"."+ "tracklog",
		// trackLog);

		return SUCCESS;
	}

	public String doubleClikShowImage() {
		return SUCCESS;
	}

	public String tracklogIframe() {
		return SUCCESS;
	}

	public Integer getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(Integer totalNum) {
		this.totalNum = totalNum;
	}

	public Integer getCPage() {
		return cPage;
	}

	public String getFirstPicPath() {
	//	firstPicPath = StringUtil.strEncode(firstPicPath);
		return firstPicPath;
	}

	public void setFirstPicPath(String firstPicPath) {
		firstPicPath = StringUtil.strEncode(firstPicPath);
		this.firstPicPath = firstPicPath;
	}

	public void setCPage(Integer page) {
		cPage = page;
	}

	public boolean isHasPrev() {
		return hasPrev;
	}

	public void setHasPrev(boolean hasPrev) {
		this.hasPrev = hasPrev;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public Integer getTracklogIdIndex() {
		return tracklogIdIndex;
	}

	public void setTracklogIdIndex(Integer tracklogIdIndex) {
		this.tracklogIdIndex = tracklogIdIndex;
	}

	public String getSearchLeastRecord() {
		return searchLeastRecord;
	}

	public void setSearchLeastRecord(String searchLeastRecord) {
		this.searchLeastRecord = searchLeastRecord;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public TrackLog getTrackLog() {
		return trackLog;
	}

	public void setTrackLog(TrackLog trackLog) {
		this.trackLog = trackLog;
	}

	public TrackLogMgr getTrackLogMgr() {
		return trackLogMgr;
	}

	public void setTrackLogMgr(TrackLogMgr trackLogMgr) {
		this.trackLogMgr = trackLogMgr;
	}

}
